package com.shen.accountbook.module.splash;

import android.Manifest;
import android.content.Context;
import android.text.TextUtils;
import android.widget.TextView;

import com.shen.accountbook.R;
import com.shen.accountbook.application.MyApp;
import com.shen.accountbook.config.Constant;
import com.shen.accountbook.module.base.BaseActivity;
import com.shen.accountbook.module.base.EmptyPresenterImpl;
import com.shen.accountbook.module.base.EmptyView;
import com.shen.accountbook.module.base.MVPActivity;
import com.shen.accountbook.module.home.main.HomeActivity;
import com.shen.accountbook.utils.FilesUtils;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by mrCTang on 2017/6/6.
 */

public class SplashScreenActivity extends MVPActivity<SplashScreenPresenterImpl> implements SplashScreenView {

    @BindView(R.id.tv_down_splash)
    TextView mTvDown;

    @Override
    protected SplashScreenPresenterImpl createPresenter(Context context) {
        return new SplashScreenPresenterImpl(context, this);
    }

    @Override
    protected int getResId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        requestPermissions();

        FilesUtils.createDir(Constant.DBDir);
        FilesUtils.copyFile(this.getDatabasePath(Constant.DB_NAME).getAbsolutePath(),
                Constant.DBDir + Constant.DB_NAME);
    }


    @Override
    public void goHome() {
        startActivity(null, HomeActivity.class);
        finishSimple();

        MyApp.showDebugDBAddressLog();
    }

    @Override
    public void setTvText(String s) {
        mTvDown.setText(s);
    }


    public void requestPermissions() {
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Observer<Boolean>() {
                               @Override
                               public void onSubscribe(Disposable d) {
                                   mPresenter.addDisposable(d);
                               }

                               @Override
                               public void onNext(Boolean aBoolean) {

                                   if(aBoolean){
                                       mPresenter.countDown();
                                       mPresenter.initDataBase();
                                   }else {
                                       finish();
                                   }
                               }

                               @Override
                               public void onError(Throwable e) {

                               }

                               @Override
                               public void onComplete() {

                               }
                           });
    }
}
